﻿#pragma once
#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#ifndef __GD__ViewComponent__
#define __GD__ViewComponent__

#include "CrossApp.h"

/* select 组件委托 */
class GDSelectViewDelegate
{
public:
	virtual ~GDSelectViewDelegate(){};

	virtual void selectOption(CAView *selectView, unsigned int index, std::string value, std::string title){};
};


/* 果冻视图组件 */
class GDViewComponent :
	public CAView
{

public:
	GDViewComponent();

	virtual ~GDViewComponent();
	virtual bool init();
protected:
	virtual void initView();
private:

protected:
	CCSize viewSize; // 视图大小
};

/* 分割线视图组件 */
class SplitLineViewComponent :
	public GDViewComponent
{
public:
	

	virtual ~SplitLineViewComponent();

	static SplitLineViewComponent* createWithFrame(const CCRect& rect, std::string title, unsigned long fontSize = 30);
	static SplitLineViewComponent* createWithCenter(const CCRect& rect, std::string title, unsigned long fontSize = 30);

	virtual bool init();
	
	virtual void setColor(const CAColor4B& color); // 设置颜色
	void setTitleColor(const CAColor4B& color); // 设置标题颜色
	void setLineColor(const CAColor4B& color); // 设置标题颜色
protected:
	virtual void initView();
private :
	SplitLineViewComponent(); // 构造函数不公开
protected:
	std::string title; // 标题
	unsigned long fontSize; // 字体大小
private :
	CAView *lineView;
	CAView *titleView;
	CALabel *titleLabel;
};


class OptionViewCell :public CAListViewCell
{
public:
	OptionViewCell();
	virtual ~OptionViewCell();

	static OptionViewCell* create(const std::string& identifier, const CADipRect& _rect = CADipRectZero);

	void setOption(std::string title, std::string value);
public:
	void initWithCell();
	void cellBtnCallback(CAControl* btn, CCPoint point);

protected:
	virtual void normalListViewCell();
	virtual void highlightedListViewCell();
	virtual void selectedListViewCell();
	virtual void disabledListViewCell();
private:
	std::string optionValue;
	CALabel* titleLabe;

};

/* 下拉选择弹出视图 */
class SelectPopView
	:public GDViewComponent
	, public CAListViewDelegate
	, public CAListViewDataSource
{
public:
	virtual ~SelectPopView();
	virtual bool init();
	static SelectPopView* create(CAView *superView, std::string title = "");
	/* 统计下拉选项数量 */
	unsigned int countOptions();
	/* 添加一个下拉选项 */
	void addOption(string title, string value);
	/* 添加多个选项 */
	void addOptions(vector<string> titles, vector<string> values);
	/* 清除所有下拉选项 */
	void clearOption();
	/* 设置当前选中的值 */
	void setSelectValue(std::string value);
	/* 设置当前选中的下拉选项 */
	void setSelectAtIndex(unsigned int index);

	void freshSelectView();
	/* 获取当前选中的下标 */
	int getCurSelectIndex();
	
	std::string getCurSelectTitle();
	std::string getCurSelectValue();
public:
	virtual void setVisible(bool visible);

	virtual unsigned int numberOfIndex(CAListView *listView);
	virtual unsigned int listViewHeightForIndex(CAListView *listView, unsigned int index);
	virtual CAListViewCell* listViewCellAtIndex(CAListView *listView, const CCSize& cellSize, unsigned int index);
	virtual void listViewDidSelectCellAtIndex(CAListView *listView, unsigned int index);
	virtual void listViewDidDeselectCellAtIndex(CAListView *listView, unsigned int index);

	CC_SYNTHESIZE(GDSelectViewDelegate*, m_SelectViewDelegate, SelectViewDelegate);
	CC_SYNTHESIZE(int, m_itemHeight, ItemHeight);
	void onCloseBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point);
protected:
	virtual void initView();
	// 重置高度
	void resetHeight();
private:
	SelectPopView(); // 构造函数不公开
private :
	std::string title;
	CALabel *titleLabel;

	CAView *rootNode;
	CAView *listDataNode;

	CAListView *listView;
	vector<string> titleList;
	vector<string> valueList;

	int curSelectIndex;
	unsigned int listMaxHeight; // list列表最大高度
};

/*
	下拉选择组件
*/
class SelectViewComponent 
	:public GDViewComponent
	, public GDSelectViewDelegate
{
public:
	~SelectViewComponent();
	virtual bool init();
	static SelectViewComponent* createWithFrame(CAViewController *controller, CCRect rect, std::string title, std::string popViewTitle = "");
	static SelectViewComponent* createWithCenter(CAViewController *controller, CCRect rect, std::string title, std::string popViewTitle = "");
	unsigned int countOptions();
	/* 添加一个下拉选项 */
	void addOption(string title, string value);
	/* 添加选项 */
	void addOptions(vector<string> titles, vector<string> values);
	/* 清除所有下拉选项 */
	void clearOption();
	void freshSelectView();
	

	/* 设置当前选中的值 */
	void setSelectValue(std::string value);
	/* 设置当前选中的下拉选项 */
	void setSelectAtIndex(unsigned int index);

	virtual void selectOption(CAView *selectView, unsigned int index, std::string value, std::string title);

	CC_SYNTHESIZE(GDSelectViewDelegate*, m_SelectViewDelegate, SelectViewDelegate);

	void onSelectBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point);

	/* 设置标题字体大小 */
	void setTitleFontSize(unsigned int fontSize);
	void setTitleColor(const CAColor4B& color);

	std::string getSelectTitle(); // 获取当前选中的标题
	std::string getSelectValue(); // 获取当前选中的值
	unsigned int getSelectIndex(); // 获取当前选中的下标
	// 设置下拉选择按钮图片
	void setImageForState(const CAControlState& controlState, CAImage* var);
	CALabel *getTitleLabel();
protected:
	void setTitle(std::string); // 设置标题
	virtual void initView(CAViewController *controller, std::string title,  std::string popViewTitle = "");
private:
	SelectViewComponent();
private:
	SelectPopView *selectPopView; // 点击时弹出的视图
	CAButton *selectBtn; // 选择按钮
	CALabel *titleLabel; // 显示的文字
	std::string defaultTitle; // 默认文字
	int tmpSelectIndex; // 记录点击下拉列表按钮前选中的索引，已便于选则后的对比
};


typedef enum
{
	GDButtonType_LOGIN_BTN = 0,         // 登录注册按钮
	GDButtonType_LEFT_BIG_BTN,          // 左侧大按钮
	GDButtonType_LEFT_KUANG_BTN,          // 左侧框按钮
	GDButtonType_GOODS_GRAB_SITE_BTN,    // 分享商品网站按钮
	GDButtonType_GOODS_GRAB_POST,    // 分享商品提交按钮
	GDButtonType_GOODS_LIST_FILTER_OK,    // 商品列表删选条件确认按钮
}GDButtonType;

class GDButton
	:public GDViewComponent
{
public :
	GDButton(int type);
	~GDButton();
	static GDButton* createWithFrame(CCRect rect, std::string title = "", GDButtonType type = GDButtonType_LOGIN_BTN);
	static GDButton* createWithCenter(CCRect rect, std::string title = "", GDButtonType type = GDButtonType_LOGIN_BTN);
	CAButton* getBtn();
	CALabel* getTitleLabel();
	void setTitle(std::string title);
	/* 设置按钮样式 */
	void setType();

	void addTarget(CAObject *target, SEL_CAControl selector, CAControlEvents event);
protected:
	virtual void initView(CCSize& size);
private:
	CAButton *btn;
	CALabel *titleLabel;
	int type;
};

#endif /* defined(__GD__ViewComponent__) */